package com.zfx.service.impl;

import com.github.pagehelper.PageHelper;
import com.zfx.mapper.IAdminMapper;
import com.zfx.pojo.Admin;
import com.zfx.service.IAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @description: 运营账号服务层实现类
 * @author: zfx
 */
@Service
public class AdminServiceImpl implements IAdminService {

    @Autowired
    private IAdminMapper adminMapper;

    @Transactional(readOnly = true)
    @Override
    public List<Admin> findAll(int page, int size) {
        PageHelper.startPage(page, size);
        return adminMapper.selectAll();
    }

    @Transactional(readOnly = true)
    @Override
    public List<Admin> findByAdmin(Admin admin, int page, int size) {
        PageHelper.startPage(page, size);
        return adminMapper.findByAdmin(admin);
    }

    @Transactional(readOnly = true)
    @Override
    public Admin login(Admin admin) {
        return adminMapper.login(admin);
    }

    @Transactional(readOnly = true)
    @Override
    public Admin findById(Integer id) {
        return adminMapper.selectByPrimaryKey(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int save(Admin admin) {
        Admin record = new Admin();
        record.setLoginId(admin.getLoginId());
        int count = adminMapper.selectCount(record);
        if(count != 0 ) {
            return -1;
        }
        return adminMapper.insert(admin);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int update(Admin admin) {
        return adminMapper.updateByPrimaryKeySelective(admin);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteById(Integer id) {
        return adminMapper.deleteByPrimaryKey(id);
    }

    @Transactional(readOnly = true)
    @Override
    public int countAll(Admin admin) {
        return adminMapper.countByAdmin(admin);
    }

}
